package com.qf.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.qf.order.client.StockClient;
import com.qf.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope   // 配置动态刷新的注解
public class OrderController {

    @Autowired
    private StockClient stockClient;

    @Value("${order-version}")
    private String version;

    @GetMapping("/order/test")    // @SentinelResource
    public String test(){
        String result = stockClient.test();
        return result + "  version：" + version;
    }

    @GetMapping("/order/test1")
    @SentinelResource(value = "test1",blockHandler = "test1Handler",fallback = "testFallback")
    public String test1(String id) throws InterruptedException {
        if("1".equalsIgnoreCase(id)){
            int i = 1 / 0;
        }else if("2".equalsIgnoreCase(id)){
            Thread.sleep(1000);
        }
        return "test1 ================== " + id;
    }


    public String test1Handler(String id, BlockException ex) throws InterruptedException {
        return "test1方法出现了BlockException，我是降级处理！！！";
    }
    public String testFallback(String id, Throwable ex) throws InterruptedException {
        return "test1方法出现了Throwable，我是降级处理！！！";
    }


    @Autowired
    private OrderService orderService;

    @GetMapping("/order/create")
    public String create(){
        orderService.create();
        return "ok";
    }

}
